package cn.wostore.wxhelper.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wostore.wxhelper.R;
import cn.wostore.wxhelper.bean.ForwardRule;
import cn.wostore.wxhelper.utils.L;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fanghui at 2019-1-31
 */
public class RuleListAdapter extends RecyclerView.Adapter {

    private Context mContext;

    private List<ForwardRule> ruleList = new ArrayList<>();

    private OnSwitchListener mOnSwitchListener;

    private OnDeleteListener mOnDeleteListener;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        mContext = parent.getContext();
        view = LayoutInflater.from(mContext).inflate(R.layout.item_rule_list, parent, false);
        return new RuleListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final RuleListViewHolder holder = (RuleListViewHolder) viewHolder;
        final ForwardRule bean = ruleList.get(position);

        holder.originalTv.setText(bean.getForwardMainGroupName());
        holder.teacherTv.setText(bean.getForwardMasterName());
        holder.destTv.setText(bean.getForwardChildGroupNames());
        if (bean.isForwardRuleState()){
            holder.switchTv.setBackground(mContext.getDrawable(R.drawable.shape_switch_on));
            holder.switchTv.setText(mContext.getText(R.string.switch_off));
        } else {
            holder.switchTv.setBackground(mContext.getDrawable(R.drawable.shape_switch_off));
            holder.switchTv.setText(mContext.getText(R.string.switch_on));
        }
        holder.switchTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (bean.isForwardRuleState()){
                    L.d("现在是开启状态");
                    holder.switchTv.setBackground(mContext.getDrawable(R.drawable.shape_switch_off));
                    holder.switchTv.setText(mContext.getText(R.string.switch_on));
                    mOnSwitchListener.onSwitchOff(bean.getForwardRuleId());
                    L.d("关闭成功");
                } else {
                    L.d("现在是关闭状态");
                    holder.switchTv.setBackground(mContext.getDrawable(R.drawable.shape_switch_on));
                    holder.switchTv.setText(mContext.getText(R.string.switch_off));
                    mOnSwitchListener.onSwitchOn(bean.getForwardRuleId());
                    L.d("开启成功");
                }
            }
        });
        holder.deleteTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnDeleteListener.onDeleteClick(bean.getForwardRuleId());
            }
        });

    }

    @Override
    public int getItemCount() {
        return ruleList.size();
    }

    /**
     * 初次加载或刷新数据
     */
    public void setData(List<ForwardRule> list) {
        this.ruleList.clear();
        this.ruleList.addAll(list);
        notifyDataSetChanged();
    }

    public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
        this.mOnSwitchListener = onSwitchListener;
    }

    public void setmOnDeleteListener(OnDeleteListener mOnDeleteListener) {
        this.mOnDeleteListener = mOnDeleteListener;
    }

    public interface OnSwitchListener{
        void onSwitchOn(int id);
        void onSwitchOff(int id);
    }

    public interface OnDeleteListener{
        void onDeleteClick(int id);
    }

    public static class RuleListViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.tv_original)
        TextView originalTv;

        @BindView(R.id.tv_teacher)
        TextView teacherTv;

        @BindView(R.id.tv_dest)
        TextView destTv;

        @BindView(R.id.tv_switch)
        TextView switchTv;

        @BindView(R.id.tv_delete)
        TextView deleteTv;

        public RuleListViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
